<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Fenyong extends Command
{
    protected function configure()
    {
        $this->setName('fenyong')->setHelp('分佣')->setDescription('订单分佣');

    }

    protected function execute(Input $input, Output $output)
    {
        $data = db('order')->where(['order_status'=>4])->select();
        if($data){
            foreach ($data as $k=>$v){
                if($v['user_rebate'] > 0){
                    $this->digui($v['user_id'], $v['user_rebate'],$v['goods_id'],$v['id']);
                }else{
                    $this->diguitwo($v['user_id'],$v['goods_id'],$v['id']);
                }
//                $kai_time = $v['jiesuan_time']+86400*3;
//                $kai_time = date("Y-m-d 18:00:00",$kai_time);
//                if(time() > strtotime($kai_time)){
//
//                }

            }
        }
        echo "操作成功";
    }


    public function diguitwo($user_id,$goods_id,$order_id){
        $userModel    = new \app\common\model\User();
        $PackagesInfo = Packages::where(['id'=>$goods_id])->find();
        $Commission   = new UserCommission();
        $config       = new UserPackagesConfig();
        if($PackagesInfo){
            $userInfo = $userModel->where(['id'=>$user_id])->find();
            if($userInfo['parent_id'] > 0){
                return $this->diguitwo($userInfo['parent_id'],$goods_id,$order_id);
            }
            $userConfig = $config->where(['user_id'=>$user_id,'packages_id'=>$goods_id])->find();
            if($userConfig){
                if($userConfig['day'] == 1){
                    $inser['settlement_time']  = strtotime(date("Y-m-d 18:00:00"));
                }else if($userConfig['day'] == 6){
                    $kai_time = time()+86400*30;
                    $kai_time = date("Y-m-d 18:00:00",$kai_time);
                    $inser['settlement_time']  = strtotime($kai_time);
                }else{
                    $kai_time = time()+86400*$userConfig['day'];
                    $kai_time = date("Y-m-d 18:00:00",$kai_time);
                    $inser['settlement_time']  = strtotime($kai_time);
                }
                $inser['user_id']         = $user_id;
                $inser['money']           = $PackagesInfo['rebate'];
                $inser['order_id']        = $order_id;
                $inser['packages_id']     = $goods_id;
                $Commission->create($inser);
            }
        }
    }

    public function digui($user_id,$money,$goods_id,$order_id,$key=0){
        $userModel    = new \app\common\model\User();
        $Commission   = new UserCommission();
        $config       = new UserPackagesConfig();
        $PackagesInfo = Packages::where(['id'=>$goods_id])->find();
        if($PackagesInfo){
            if($money > 0){
                $userInfo   = $userModel->where(['id'=>$user_id])->find();
                $userConfig = $config->where(['user_id'=>$user_id,'packages_id'=>$PackagesInfo['id']])->find();
                if($userConfig){
                    if($userConfig['day'] == 1){
                        $inser['settlement_time']  = strtotime(date("Y-m-d 18:00:00"));
                    }else if($userConfig['day'] == 6){
                        $kai_time = time()+86400*30;
                        $kai_time = date("Y-m-d 18:00:00",$kai_time);
                        $inser['settlement_time']  = strtotime($kai_time);
                    }else{
                        $kai_time = time()+86400*$userConfig['day'];
                        $kai_time = date("Y-m-d 18:00:00",$kai_time);
                        $inser['settlement_time']  = strtotime($kai_time);
                    }
                    $inser['user_id']         = $user_id;
                    $inser['money']           = $money;
                    $inser['order_id']        = $order_id;
                    $inser['packages_id']     = $goods_id;
                    $Commission->create($inser);
                }
                $key +=1;
                if($userInfo['parent_id'] > 0){
                    $configInfo = $config->where(['user_id'=>$userInfo['parent_id'],'packages_id'=>$PackagesInfo['id']])->find();
                    if($configInfo){
                        $money = $configInfo['money'] - $money;
                    }
                    return $this->digui($userInfo['parent_id'], $money,$goods_id,$order_id,$key);
                }
            }
        }
    }

}
